#include <windows.h>
#include "tronGame.h"
 
#define WIDTH 640
#define HEIGHT 480

//Globals///////////////////////////////////////////////////
HINSTANCE hInst;
HWND wndHandle;
tronGame game;
////////////////////////////////////////////////////////////

//Function Prototypes///////////////////////////////////////
bool    initWindow(HINSTANCE hInstance);
LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
////////////////////////////////////////////////////////////

//WinMain
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE prevInst, LPTSTR lpCmdLine, int nCmdShow)
{
	//Initialize the window that our app will exist in
	if (!initWindow(hInst))
	{
		MessageBox(NULL, "Unable to create window", "ERROR", MB_OK);
		return 0;
	}

	if(!game.init(wndHandle))
	{
		MessageBox(NULL, "DirectX failed to initialize", "ERROR", MB_OK);
		return 0;
	}

	MSG msg;
    ZeroMemory(&msg, sizeof(msg));

	// Game loop
	while(msg.message != WM_QUIT)
	{
		// check for messages
		if( PeekMessage( &msg, NULL, 0U, 0U, PM_REMOVE ) )
        {
			TranslateMessage( &msg );
            DispatchMessage( &msg );
        }
		else
		{
			game.updateScene();
			game.drawScene();
			Sleep(1);
		}
	}

	//Done with the program. Clean up our mess.
	game.cleanUp();

	return (int)msg.wParam;
}

//Creates the window our game lives in.
bool initWindow(HINSTANCE hInstance)
{
	WNDCLASSEX wcex;
	DWORD      m_dwWindowStyle;			// this holds the window style

	wcex.cbSize = sizeof(WNDCLASSEX);
	wcex.style			= CS_HREDRAW | CS_VREDRAW;
	wcex.lpfnWndProc	= (WNDPROC)WndProc;
	wcex.cbClsExtra		= 0;
	wcex.cbWndExtra		= 0;
	wcex.hInstance		= hInstance;
	wcex.hIcon			= 0;
	wcex.hCursor		= LoadCursor(NULL, IDC_ARROW);
	wcex.hbrBackground	= (HBRUSH)(COLOR_WINDOW+1);
	wcex.lpszMenuName	= NULL;
	wcex.lpszClassName	= "TRON_class";
	wcex.hIconSm		= 0;
	RegisterClassEx(&wcex);

	m_dwWindowStyle = WS_OVERLAPPEDWINDOW;

	//create window
	wndHandle = CreateWindow("TRON_class", 
							 "TRON", 
							 m_dwWindowStyle,	// pass the window style as a variable this time
							 CW_USEDEFAULT, 
							 CW_USEDEFAULT, 
							 WIDTH, 
							 HEIGHT, 
							 NULL, 
							 NULL, 
							 hInstance, 
							 NULL);
   if (!wndHandle)
      return false;
   
   ShowWindow(wndHandle, SW_SHOW);
   UpdateWindow(wndHandle);

   return true;
}

//Window Process Callback function.
LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
	switch (message) 
	{
		//When the window is closed.
		case WM_DESTROY:
			PostQuitMessage(0);
			break;

		//If a key is pressed down.
		case WM_KEYDOWN:
			switch(wParam)
			{
			//The W key or the Up key.
			case 'W':
			case VK_UP:
				MessageBox(0, "You hit W or Up arrow", "You hit a button!?!?!?!?", MB_OK);
				break;
			//The A key or the Left key.
			case 'A':
			case VK_LEFT:
				MessageBox(0, "You hit A or Left arrow", "You hit a button!?!?!?!?", MB_OK);
				break;
			//The S key or the Down key.
			case 'S':
			case VK_DOWN:
				MessageBox(0, "You hit S or Down arrow", "You hit a button!?!?!?!?", MB_OK);
				break;
			//The D key or the Right key.
			case 'D':
			case VK_RIGHT:
				MessageBox(0, "You hit D or Right arrow", "You hit a button!?!?!?!?", MB_OK);
				break;
			case VK_SPACE:
				game.startGame();
				break;
			//The Escape key.
			case VK_ESCAPE:
				PostQuitMessage(0);
				break;
			}
			break;
	}
	//Isn't handled by our program. Have Windows take care of it.
	return DefWindowProc(hWnd, message, wParam, lParam);
}